package com.gdut.huayan.config;

import com.alibaba.fastjson.JSONObject;
import com.gdut.huayan.validation.NoLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Aspect
@Component
public class WebLogAspect {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("execution( * com.gdut.huayan.controller..*.*(..))")
    public void log() {
    }

//    public String getRealIpAddress(HttpServletRequest request) {
//        String headerIp = request.getHeader("X-Real-IP");
//        if (headerIp == null || headerIp.equals("") || headerIp.equals("127.0.0.1")) {
//            headerIp = request.getHeader("X-Forwarded-For");
//        }
//        return headerIp;
//    }

    @Before("log()")
    public void webLog(JoinPoint joinPoint) throws NoSuchMethodException {
        if(((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(NoLog.class) != null){
            return;
        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
//        System.out.println("ServletRequestAttributes");
        HttpServletRequest request = attributes.getRequest();
        //通过请求获取url,ip
        String url = request.getRequestURL().toString();
        //String ip = getRealIpAddress(request);
        String ip = "127.0.0.1";
        //获取方法所在的包名和类名
        String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();

        //获取请求的参数
        Object[] args = joinPoint.getArgs();
//        RequestLog requestLog = new RequestLog(url, ip, classMethod, args);
//        System.out.println("]"+url+"ip"+ip+"classmethod"+classMethod);
//        logger.info("Request[{}]",
//                JSONObject.toJSONString(Map.of("url",url,"ip",ip,"classMethod",classMethod,"args",args)));
    }

    @AfterReturning(returning = "result", pointcut = "log()")
    public void doAfterReturn(Object result) {
        logger.info("Result[{}]", JSONObject.toJSONString(result));
    }

    private static class RequestLog {
        private final String url;         //请求的url
        private final String ip;          //请求ip
        private final String classMethod; //请求方法
        private final Object[] args;      //请求参数

        public RequestLog(String url, String ip, String classMethod, Object[] args) {
            this.url = url;
            this.ip = ip;
            this.classMethod = classMethod;
            this.args = args;
        }

    }
}
